vimfandomcom-20200223-history
Keystroke Saving Substituting and Searching
Saving Keystrokes for common Substituting and Searching a) Searching: Sorry, there is not much that can be saved for common Searching. It's just hitting /mypattern b) Substituting: I think, common substitution requires pretty many keystrokes. So I use the following macro with my favorite substitution options: :map :%s///gc This ends up with the cursor after the first '/' in the commandline. To complete it, you only have to enter -> myoldpattern/mynewpattern Remark: I mapped it to (cause of tribute to the of the good old Norton Commander editor). You may map it where you want to. Searching for resp. Substituting of the current word under the cursor a) Searching: If you don't know how to look for the next occurrence of the word under the cursor, you should see VimTip1 or . b) Substituting: The following macro extends the one above with automatically inserting the current word under the cursor into the from - pattern of the :s command. :map :%s///gc To complete it, just enter -> mynewpattern I use this i.e. for reliable and quickly renaming a variable in the entire buffer. I mapped it to Shift-. You may map it to the keystroke you want. Explanation: CTRL-r+CTRL-w expands to the word under the cursor. Searching and Substituting for an arbitrary visually selected part of text If you want to look or substitute (for) an arbitrary pattern (which already exists at least once in your text), the following 2 mappings do it for you. The advantage is that you don't have to type again or cut and paste the appropriate text but only have to visually select it. a) Searching: :vmap / y:execute "/".escape(@",'[]/\.*') This immediately finds to the next occurrence of the previously visually selected text. b) Substituting: :vmap y:execute "%s/".escape(@",'[]/')."//gc" Again, as in the mapping in chapter 2), you just have to complete it by entering -> mynewpattern Explanation/Discussion: What both Substituting and Searching in this way generally does is: Yank the selected text. Inserting the visually selected via addressing the '"' register with '@"' as a parameter of the escape() function going finally into the 'myoldpattern' part. The tricky problem is, if you have characters in your myoldpattern, which are regular expression chars, they are recognized and treated accordingly. That is most likely not what you wanted. To escape them, these chars have to be declared by the second parameter of the excape() function, which then escapes them with a backslash. The few characters above work for me. If you run into problems, you should check for additional regexp chars in your text, and try to escape them by adding them to the escape() function parameter. Conclusion With the appropriate mappings in your vimrc you can save keystrokes when Searching or Substituting and avoid typing errors. That way, you can take lunch sooner. Comments Great to find escape() I have the following map, as a consequence: cm =escape(@",'[]/\.*') vm * y/| vm / y:%s// vm # y?| vm ? y:g//t$ ----